#!/bin/bash

current_server=$(head -n1 /data/emby_server.txt)
_docker_address=$(head -n1 /data/docker_address.txt)

sed -i "s#\$emby http://[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*:[0-9]*;#\$emby $current_server;#g" /etc/nginx/http.d/emby.conf

port=$(echo $current_server | cut -d ':' -f 3)
cp /etc/nginx/http.d/emby.js /etc/nginx/http.d/emby.js.$(date +%Y%m%d)
cp /etc/nginx/http.d/externalPlayer.js /etc/nginx/http.d/externalPlayer.js.$(date +%Y%m%d)
if [ "$port" == "6909" ]; then
    cp /var/lib/eb.js /etc/nginx/http.d/emby.js
    cp /var/lib/ep.js /etc/nginx/http.d/externalPlayer.js
	infuse_api_key=$(head -n1 /data/infuse_api_key.txt)
else
    unzip -o /var/lib/data.zip emby.js externalPlayer.js -d /etc/nginx/http.d/
	infuse_api_key="e825ed6f7f8f44ffa0563cddaddce14d"
fi

sed -i "s#EMBY_SERVER#$current_server#g" /etc/nginx/http.d/emby.js
sed -i "s#_DOCKER_ADDRESS#$_docker_address#g" /etc/nginx/http.d/emby.js
sed -i "s#INFUSE_API_KEY#$infuse_api_key#g" /etc/nginx/http.d/emby.js

nginx -s reload
